<?php

  /**
   * @author Dany Mottier
   * @version 2.2.2
   */
  class HttpPost extends Socket {

    /** @var IString The url to join for executing a script. */
    private $url = "";

    /** @var IString The stream get from the target script. */
    private $response = "";

    /** @var array The data to post to the script. */
    private $data = array();

    /**
     * Store all parameters in private variables for further use.<br />
     *
     * @param string $url The url.
     * @param string $ip The hostname.
     * @param integer $port The port.
     * @param integer $timeout The timeout.
     */
    public function __construct($url, $hostname = "127.0.0.1", $port = 80, $timeout = 30) {
      parent::__construct($hostname, $port, $timeout);
      $this->url = new String($url);
    }

    /**
     * Returns the url used.
     * @return IString The url.
     */
    public function getUrl() {
      return $this->url;
    }

    /**
     * Sets the url to post.
     * @param string $value The url.
     */
    public function setUrl($value) {
      $this->url = new String($value);
    }

    /**
     * Add a pair key/value in the HTTP POST stream protocol.
     * @param IString $key The name of the variable to post.
     * @param IString $value The value of the variable to post.
     */
    public function addData(IString $key, IString $value) {
      $this->data[$key->toString()] = $value->toString();
    }

    /**
     * Returns the value of a set key.
     * @param IString $key The string index of the value to get.
     * @return IString The value at the $key index.
     */
    public function getData(IString $key) {
      return new String($this->data[$key->toString()]);
    }

    /**
     * Posts all set variables to the host url.
     * @return IBoolean True if success, otherwise, false.
     * @link http://be2.php.net/manual/en/function.urlencode.php
     * @link http://be2.php.net/manual/en/function.strlen.php
     */
    public function post() {
      if (!$this->isConnected()->booleanValue()) {
        $this->connect();
      }

      $data = "";

      foreach ($this->data as $key => $value) {
        $data .= $key . "=" . urlencode($value) . "&";
      }

      $out = "POST " . $this->url->toString() . " HTTP/1.1\r\n";
      $out .= "Host: " . $this->getHostname()->toString() . "\r\n";
      $out .= "Connection: Close\r\n";
      $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
      $out .= "Content-Length: " . strlen($data) . "\r\n\r\n";
      $out .= $data . "\r\n";

      $this->write(new String($out));

      while (!$this->endOfStream()->booleanValue()) {
        $this->response .= $this->read(new Integer(1024));
      }
      return $this->disconnect();
    }

    /**
     * Returns the entire response.
     * @return IString The entire response with HTTP headers.
     */
    public function getResponse() {
      return new String($this->response);
    }

    /**
     * Return the parsed response.
     * @return IString The parsed response of the host without HTTP header.
     * @link http://be2.php.net/manual/en/function.explode.php
     */
    public function getContentResponse() {
      $data = explode("\r\n\r\n", $this->response);

      return new String($data[1]);
    }

  }

?>
